package com.yc.jpyy.usbkey.controller;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.company.controller.CompanyController;
import com.yc.jpyy.company.service.CompanyService;
import com.yc.jpyy.usbkey.service.UsbkeyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created by thinkpad on 2018/8/11.
 */
@Controller
@RequestMapping(value = "/usbkey")
public class usbkeycontroller extends BaseController {
    @Autowired
    private UsbkeyService usbkeyService;
    private static Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @SuppressWarnings("finally")
    @RequestMapping(value = "/queryUsbkeyInfo", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json; charset=utf-8")
    public @ResponseBody
    String findCompanyByID(HttpServletRequest request) throws UnsupportedEncodingException {
        RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
        try {
            Map<String, Object> paras = new HashMap<String, Object>();
            paras.put("userId",request.getParameter("userId"));
            paras.put("mac",request.getParameter("mac"));
            Map<String, Object> val = usbkeyService.queryUsbkeyInfo(paras);
            //todo 测试用
            //response.setUsbkeyInfo(request.getAttribute("usbkeyInfo"));
            response.setReturnMsg(1, "查询单条数据成功");
            response.setListData(val);
            return response.toJSON();
        } catch (Exception e) {
            logger.info("查询单条数据异常" + e.toString());
            // 异常信息
            response.setReturnCode(0);
            response.setReturnMsg("查询单条数据异常");

        } finally {
            return response.toJSON();
        }
    }
}
